package MIPush.messagefactory;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Message.Builder;

import MIPush.MessageInfo;

public class AndroidMessageFactory extends MessageFactory {

	protected AndroidMessageFactory() {
	}

	/**
	 * 构建消息对象
	 * 
	 * @param equipment
	 * @param messageInfo
	 * @return
	 */
	@Override
	public Message createMessage(MessageInfo messageInfo) {
		Message message = null;
		Builder builder = new Builder();
		builder.title(messageInfo.getTitle());
		builder.description(messageInfo.getDescription());
		builder.payload(messageInfo.getPayload());
		Map<String, String> extra = messageInfo.getExtra();
		if (extra != null && extra.size() > 0) {
			Set<Entry<String, String>> entrySet = extra.entrySet();
			for (Entry<String, String> entry : entrySet) {
				builder.extra(entry.getKey(), entry.getValue());
			}
		}
		builder.passThrough(messageInfo.getPassThrough()); // 消息使用透传方式
		builder.notifyType(messageInfo.getNotifyType()); // 使用默认提示音提示
		List<String> packageNames = messageInfo.getPackageNames();
		if (packageNames != null && packageNames.size() > 0) {
			if (packageNames.size() == 1) {
				builder.restrictedPackageName(packageNames.get(0));
			} else {
				builder.restrictedPackageNames((String[]) packageNames.toArray());
			}
		}
		builder.timeToLive(messageInfo.getMilliseconds());
		builder.timeToSend(messageInfo.getTimeToSend());
		Integer notifyId = messageInfo.getNotifyId();
		if (notifyId != null) {
			builder.notifyId(notifyId);
		}
		builder.enableFlowControl(messageInfo.isNeedFlowControl());
		message = builder.build();
		return message;
	}
}
